Product Code Database
Example Keywords: strategy games -mario $43
   » » Wiki: Control Table
Tag Wiki 'Control Table'.
Tag

A control table is a table used to direct the of a . that uses a control table is said to be table-driven. Programs from decision tables, Humby, E., 2007, Macdonald, 1973 ... Biggerstaff, Ted J. Englewood Cliffs, NJ : Prentice-Hall A control table encodes both the parameters to a conditional expression and a function reference. An interpreter processes a table by evaluating the conditional expression for input data and invoking the selected function. Using a control table can reduce the need for repetitive code that implements the same logic.

In general, the mapping of input parameters can be via any . A common data structure is the , which provides relatively high performance but at a relatively high memory footprint. An associative array can minimize memory use at the cost of more lookup time.

How the associated behavior is referenced varies. Some languages provide a direct function reference (i.e. pointer) that can be used to invoke a function directly, but some languages do not. Some languages provide for to a location (i.e.label). As a fallback, any language allows for mapping input to an index that can then be used to branch to a particular part of the code.

A control table is often used as part of a higher-level algorithm. It can control the of an event-driven program. A relatively advanced use is instructions for a similar to but usually with operations implied by the table structure itself instead of encoded in the table data.


Data structure
A table can be structured in a variety of ways. It may have one or multiple dimensions and be of fixed or variable length. The structure of the table may be similar to a multimap associative array, where a data value (or combination of data values) may be mapped to one or more functions to be performed. Often, the structure allows tabular data is portable between computer platforms as long as a compatible interpreter exists on each platform.


Trivial hash function
A relatively simple implementation consists of a , one-dimensional array of values. The index space is fully covered by the array such that lookup involves indexing into the array by the input value, and a value is found even for an index that is not intended to be used; this prevents an error that might otherwise occur for an unused index value. The lookup is achieved in , without searching. In most architectures, this can be accomplished in two or three machine instructions. The technique is known as a "trivial hash function" or, when used specifically for branch tables, "".

To be feasible, the range of index values should be relatively small. In the example below, the control table is indexed by ASCII value so it has 256 entries (an entry for each ASCII value); omitted entries are shown as '...'. Only the values for the letter A, D, M, and S are important. All other values are uninteresting and set to 0. A two-byte index would require a minimum of 65,536 entries to handle all input possibilities which might consume more memory than is considered worth the value it provides.

+
0
0
1
0
4
0
3
0
2
0
0

In automata-based programming and pseudoconversational transaction processing, if the number of distinct program states is small, a "dense sequence" control variable can be used to efficiently dictate the entire flow of the main program loop.


Branch table
A is a one-dimensional array of branch/jump instructions to effect a to a program label when branched into by an immediately preceding, and indexed branch. It is sometimes generated by an optimizing compiler to execute a – provided that the input range is small and dense, with few gaps.http://www.netrino.com/node/137 (as created by the previous array example)

Although quite compact – compared to the multiple equivalent If statements – the branch instructions still carry some redundancy, since the branch and condition code mask are repeated alongside the branch offsets. Control tables containing only the offsets to the program labels can be constructed to overcome this redundancy (at least in assembly languages) and yet requiring only minor execution time overhead compared to a conventional branch table.


Decision table
Often, a control table implements a , which like a control table contains (often implied) propositions, and one or more associated actions.

A control table can act like a or more generally as a nested construct that includes logical predicates (using boolean style AND/OR conditions) for each case. Such a control table provides for a language-independent implementation of what otherwise is a language-dependent construct. The table embodies the of a program, stripped of programming language syntax and platform dependent aspects, and condensed to data and implied logic. The meaning of the table includes implied operations instead of being explicit as in a more typical programming paradigm.

Typically, a two-dimensional control table contains value/action pairs and may additionally contain operators and information such as the location, size and format of input or output data, whether is required. The table may contain or relative or absolute pointers to generic or customized primitives or functions to be executed depending upon other values in the row.

The type of values used in a control table depends on the computer language used for the interpreter. Assembly language provides the widest scope for including for lookup values. Typically, a control table contains values for each possible matching class of input together with a corresponding pointer to an action function. For a language without pointer support, the table can encode an index which can imply an offset value that can be used to accomplish the same as a pointer.


Storage
A control table can be stored in a variety of ways. It can be stored as part of a program, for example as a . It can be stored in the , for example as a or in a . It can be built dynamically by the program. For efficiency, the table should be resident in program memory when the interpreter uses it.


Interpreter
A control table interpreter executes the operations as selected by input parameters. The table and resulting runtime behavior can be modified without modifying the interpreter. An interpreter can require maintenance to change its behavior, but hopefully the interpreter is designed to support future functionality via table changes instead.

Handler functions often are coded in the same language as the interpreter, but could be in another language provided a suitable inter-language call-linkage mechanism exists. The choice of language for the interpreter and handler functions usually depends on how portable it needs to be across various platforms. There may be several versions of the interpreter to support . A subordinate control table pointer may optionally substitute for a function pointer in the action column if the interpreter supports this construct. This represents a conditional drop to a lower logical level, mimicking structured programming.


Performance considerations
Even though using a control table entails the development cost and deployment size of an interpreter, by separating (encapsulating) the executable coding from the logic, as expressed in the table, it can be more readily targeted to perform its function. This may be experienced in a application where the underlying spreadsheet software transparently converts complex logical formulae in the most efficient manner it is able, in order to display its results.

The examples below have been chosen partly to illustrate potential performance gains that may not only compensate significantly for the additional tier of abstraction, but also improve upon – what otherwise might have been – less efficient, less maintainable and lengthier code. Although the examples given are for a 'low level' assembly language and for the C language, it can be seen, in both cases, that very few lines of code are required to implement the control table approach and yet can achieve very significant performance improvements, reduce repetitive source coding and aid clarity, as compared with conventional program language constructs.


Examples

General
CT1 is a control table that is a simple . The first column represents the input value to be tested (by an implied 'IF input = x') and, if so, the action in the corresponding 2nd column is invoked. It is like a with return, a form of .

>
+ CT1 ! input !! action
Add
Subtract
Multiply
Divide

The next example illustrates how a similar effect can be achieved in a language that does not support pointer definitions in data structures but does support indexed branching to a function – contained within a (0-based) array of function pointers. The table (CT2) is used to extract the index (from 2nd column) to the pointer array (CT2P). If pointer arrays are not supported, a SWITCH statement or equivalent can be used to alter the control flow to one of a sequence of program labels (e.g.: case0, case1, case2, case3, case4) which then either process the input directly, or else perform a call (with return) to the appropriate function.

>
+ CT2 ! input!! subr #
1
2
3
4
As in above examples, it is possible to efficiently translate the potential input values (A, S, M, D) into a pointer array index without actually using a table lookup, but is shown here as a table for consistency with the first example.

>
+ CT2P ! index array
Add
Subtract
Multiply
Divide

A two-dimensional control table could be used to support testing multiple conditions or performing more than one action. An action can include a pointer to another subordinate control table. The simple example below has had an implicit 'OR' condition incorporated as an extra column (to handle lower case input, however in this instance, this could equally have been handled by having an extra entry for each of the lower case characters specifying the same function identifier as the upper case characters). An extra column to count the actual run-time events for each input as they occur is also included.

>
+ CT3 ! input 1!!alternate!! subr # !! count
0
0
0
0

The control table entries are then much more similar to conditional statements in procedural languages but, crucially, without the actual (language dependent) conditional statements (i.e. instructions) being present (the generic code is physically in the interpreter that processes the table entries, not in the table itself – which simply embodies the program logic via its structure and values).

In tables such as these, where a series of similar table entries defines the entire logic, a table entry number or pointer may effectively take the place of a in more conventional programs and may be reset in an 'action', also specified in the table entry. The example below (CT4) shows how extending the earlier table, to include a 'next' entry (and/or including an 'alter flow' (jump) function) can create a . (This example is actually not the most efficient way to construct such a control table but, by demonstrating a gradual 'evolution' from the first examples above, shows how additional columns can be used to modify behaviour.) The fifth column demonstrates that more than one action can be initiated with a single table entry – in this case an action to be performed after the normal processing of each entry ('-' values mean 'no conditions' or 'no action').

Structured programming or "Goto-less" code, (incorporating the equivalent of 'do-while' or '' constructs), can also be accommodated with suitably designed and 'indented' control table structures.

{ class="wikitable" + CT4 ! input 1!!alternate!! subr # !! count !! jump
1
|
+ CT4P ! indexpointer
Default
Add
Subtract
Multiply
Divide
Read Input1
Alter flow
End
|}


C language
The following example in C, uses two arrays that together form a table data structure. The array is a simple , one-dimensional array for obtaining the index of the item that matches the input, . The array contains addresses of labels to jump to. The loop tests each item against and if they match, invokes for the label at the same index as the found item.

static const char ops4 = { "A", "S", "M", "D" }; static const void* handler_labels4 = { &&Add, &&Subtract, &&Multiply, &&Divide };

for (int i = 0; i < sizeof(ops); i++) {

   if (op == ops[i]) {
       goto *handler_labels[i];
   }
     
}

This would execute faster if a 256 byte table is used to translate the ASCII input () to an index without searching. It would execute in for all values of . If a item contained the name of a function instead of a label, the jump could be replaced with a dynamic function call, eliminating the switch-like but decreasing runtime performance.

static const void* handler_labels = {&&Default, &&Add, &&Subtract, &&Multiply, &&Divide}; static const char handler_label_index_by_op256 = {

   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 1, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0,
   0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
     
};

size_t i = handler_label_index_by_op(x); goto *handler_labelsi;


Table-driven rating
In the specialist field of telecommunications rating (concerned with determining the cost of a particular call), table-driven rating techniques illustrate the use of control tables in applications where the rules may change frequently because of market forces. The tables that determine the charges may be changed at short notice by non-programmers in many cases.Carl Wright, Service Level Corpo. (2002) Program Code Based vs. Table-driven vs. Rule-Based Rating, Rating Matters issue n. 12, 13 November 2002 Brian E. Clauser, Melissa J. Margolis, Stephen G. Clyman, Linette P. Ross (1997) Development of Automated Scoring Algorithms for Complex Performance Assessments: A Comparison of Two Approaches Journal of Educational Measurement, Vol. 34, No. 2 (Summer, 1997), pp. 141–161

If the algorithms are not pre-built into the interpreter (and therefore require additional runtime interpretation of an expression held in the table), it is known as "Rule-based Rating" rather than table-driven rating (and consequently consumes significantly more overhead).


Spreadsheets
A could be used as a two-dimensional control table, with the non-empty cells representing data to the underlying spreadsheet program (the interpreter). The cells containing formula are usually prefixed with an equals sign and simply designate a special type of data input that dictates the processing of other referenced cells – by altering the control flow within the interpreter. It is the externalization of formulae from the underlying interpreter that clearly identifies both spreadsheets, and the above cited "rule based rating" example as readily identifiable instances of the use of control tables by non-programmers.


Programming paradigm
If the control tables technique could be said to belong to any particular programming paradigm, the closest analogy might be automata-based programming or "reflective" (a form of – since the table entries could be said to 'modify' the behaviour of the interpreter). The interpreter itself however, and the functions, can be programmed using any one of the available paradigms or even a mixture. The table itself can be essentially a collection of "" values that do not even need to be compiled and could be read in from an external source (except in specific, platform-dependent implementations using memory pointers directly for greater efficiency).


Virtual machine
A two-dimensional control table is similar to intermediate (i.e. ) running on a , in that a platform-dependent interpreter is required to perform execution that is largely determined by table content. There are also similarities to Common Intermediate Language (CIL) in the aim of creating a common intermediate instruction set that is independent of platform but unlike CIL, no pretensions to be used as a common resource for other languages. can also be considered a similar but earlier implementation with origins as far back as 1966.


Instruction fetch
When a two-dimensional control table is used to determine program flow, the normal "hardware" function is effectively simulated with either a pointer to the first (or next) table entry or else an to it. "Fetching" the instruction involves decoding the data in that table entry – without necessarily copying all or some of the data within the entry first. Programming languages that are able to use pointers have the dual advantage that less overhead is involved, both in accessing the contents and also advancing the counter to point to the next table entry after execution. Calculating the next 'instruction' address (i.e. table entry) can even be performed as an optional additional action of every individual table entry allowing and or jump instructions at any stage.


Monitoring control table execution
The interpreter program can optionally save the program counter (and other relevant details depending upon instruction type) at each stage to record a full or partial trace of the actual program flow for purposes, hot spot detection, analysis and performance analysis (see examples CT3 & CT4 above).


Advantages
Clarity
information tables are ubiquitous and mostly inherently even by the (especially tables in )

Portability
can be designed to be language and platform independent except for the interpreter

Flexibility
ability to execute either primitives or functions transparently and be custom designed to suit the problem

Compactness
table usually shows condition/action pairing side-by-side (without the usual platform/language implementation dependencies), often also resulting in reduced binary file size due to less duplication of instructions, reduced source code size due to eliminating conditional statements and reduced program load (or download) speeds

Maintainability
tables often reduce the number of source lines needed to be maintained v. multiple compares

Locality of reference
compact table structures result in tables remaining in cache

Code re-use
the interpreter is usually reusable. Frequently it can be adapted to new programming tasks using the same technique and can grow organically, becoming, in effect, a of tried and tested functions, controlled by the table definitions.

Efficiency
system wide optimization possible. Any performance improvement to the interpreter usually improves all applications using it (see examples in 'CT1' above).

Extensible
new instructions can be added by extending the interpreter

Optionally:

  • Interpreter can be and "self optimize" using runtime collected within the table itself (see CT3 and CT4 – with entries that could be periodically sorted by descending count). The interpreter can also optionally choose the most efficient lookup technique dynamically from metrics gathered at run-time (e.g. size of array, range of values, sorted or unsorted)
  • : common functions can be pre-loaded and less common functions fetched only on first encounter to reduce usage. In-table can be employed to achieve this.
  • The interpreter can have debugging, trace and monitor features built-in – that can then be switched on or off at will according to test or 'live' mode
  • Control tables can be built 'on-the-fly' (according to some user input or from parameters) and then executed by the interpreter (without building code literally).


Disadvantages
Training
application programmers are not usually trained to produce generic solutions

Computational overhead
some increase because of extra level of indirection caused by virtual instructions having to be 'interpreted' (this however can usually be more than offset by a well designed generic interpreter taking full advantage of efficient direct translate, search and conditional testing techniques that may not otherwise have been utilized)

Complexity
Complex expressions cannot always be used directly in data table entries for comparison purposes. These intermediate values can however be calculated beforehand instead within a function and their values referred to in the conditional table entries. Alternatively, a function can perform the complete complex conditional test (as an unconditional 'action') and, by setting a as its result, it can then be tested in the next table entry. See Structured program theorem


Quotations

See also

Notes


External links

Page 1 of 1
1
Page 1 of 1
1

Account

Social:
Pages:  ..   .. 
Items:  .. 

Navigation

General: Atom Feed Atom Feed  .. 
Help:  ..   .. 
Category:  ..   .. 
Media:  ..   .. 
Posts:  ..   ..   .. 

Statistics

Page:  .. 
Summary:  .. 
1 Tags
10/10 Page Rank
5 Page Refs
1s Time